package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CheckDao;
import dao.WaresDao;
import dao.impl.ICheckDao;
import dao.impl.IWaresDao;
import entity.User;
import entity.WaresCar;

public class BuyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<WaresCar> list = new ArrayList<WaresCar>();
		WaresDao wd = new IWaresDao();
		String wid = request.getParameter("wid");
		String username = (String)request.getSession().getAttribute("username");
		
		if (username!=""&&username!=null) {
			CheckDao cd= new ICheckDao();
			User u = cd.FindUser(username);
			request.getSession().setAttribute("user", u);
			
		int id=0;
		if (wid!=""&&wid!=null) {
			
//		boolean boo =	wd.SelectCar((long)Integer.parseInt(wid), username);
			
			id = Integer.parseInt(wid);
			boolean flag = wd.InputCart(id, username);
		}
		
				list = wd.ShowCar();
				request.getSession().setAttribute("list",list);
				request.getRequestDispatcher("buy.jsp").forward(request, response);

		
		
		}else{
			response.sendRedirect("login.jsp");
		}
	}

}
